import school30.*;
import school30.ui.*;

public class SquareApp extends School30App {
    public Object lock = new Object();
    private Painter painter;
    public boolean destroying = false;

    public Color[] colors = {
            new Color((byte)255, (byte)0, (byte)0),
            new Color((byte)0,  (byte)255, (byte)0),
            new Color((byte)255, (byte)0, (byte)255)
    };

    public SquareApp() {
        destroying = false;
        painter = getPainter();
    }
    private int xOrg, yOrg;

    public void startApp() {
        xOrg=getWidth()/2-5;
        yOrg=getHeight()/2-5;
        System.out.println("xOrg "+xOrg);
        System.out.println("yOrg "+yOrg);

        int i = 0;
        synchronized (lock) {
            while (!destroying) {
                System.out.println("Painting: "+i);
                painter.setColor(0,0,0);
                painter.fillRectangle(0, 0, getWidth(), getHeight());

                painter.setColor(colors[i].Red, colors[i].Green, colors[i].Blue);
                painter.drawRectangle(xOrg+i, yOrg+i, 10-2*i, 10-2*i);
                try {
                    lock.wait(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                i++;
                if (i==colors.length)
                    i=0;
            }

            System.out.println("Destroying!!!!");
        }
    }

    public void terminateApp() {
        synchronized (lock) {
            destroying = true;
            lock.notify();
        }
    }

   public void keyPressed(int kc) {
     // do nothing
	 destroying = true;
	 System.out.println ("Pressed key: "+kc+"\n");
   }

   public void keyReleased(int kc) {
     // do nothing
   }


}
